-- CS-015-09
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[GetImporteVencidoCxC](@ImporteVencido decimal(18,6) output)
AS
BEGIN
	SET NOCOUNT ON;
	select @ImporteVencido = a.T025Importe - sum(b.T029Anticipo) 
	from T025 a
	left join T029 b on a.T025Id=b.T029Id
	inner join T028 c on a.T011Entity=c.T011Entity
	where dateadd(day,isnull(T028DiasCredito,0),a.T025FechaIngreso)<=getdate()
	group by a.T025NoFactura,a.T025Importe
END
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[GetImporteVencidoCxP](@ImporteVencido decimal(18,6) output)
AS
BEGIN
	SET NOCOUNT ON;
	select @ImporteVencido = a.T026Importe - sum(b.T030Anticipo) 
	from T026 a
	left join T030 b on a.T026Id=b.T026Id
	inner join T028 c on a.T011Entity=c.T011Entity
	where dateadd(day,isnull(T028DiasCredito,0),a.T026FechaIngreso)<=getdate()
	group by a.T026NoFactura,a.T026Importe
END
GO

DECLARE @T027IdScript as varchar(80)
DECLARE @T027Descripcion as varchar(360)
SET @T027IdScript='CS-015-09'
SET @T027Descripcion='SP para obtener el saldo vencido de cxc y de cxp'
INSERT INTO T027(T027IdScript,T027Descripcion,T027FechaRealizado) VALUES (@T027IdScript,@T027Descripcion,getdate())